package com.zyx.controller;

import com.alibaba.fastjson.JSON;
import com.zyx.mapper.CollectMapper;
import com.zyx.pojo.Auto;
import com.zyx.pojo.Collect;
import com.zyx.pojo.Name;
import com.zyx.pojo.User;
import com.zyx.service.AutoService;
import com.zyx.service.CollectService;
import com.zyx.service.UserService;
import com.zyx.vo.GetSession;
import com.zyx.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("User")
public class UserController {
    @Resource
    private AutoService autoService;

    @Resource
    private UserService userService;

    @Resource
    private CollectService collectService;

    @RequestMapping(value = "query", method = RequestMethod.POST)
    public List<Auto> queryName(String autoName) {
        List<Auto> list = new ArrayList<>();

        return list;
    }

    /*@ResponseBody
    @RequestMapping(value = "addSubscribe",method = RequestMethod.POST)
    public ResultVo addSubscribe(){

    }*/

    //添加收藏
    @ResponseBody
    @RequestMapping(value = "addCollect", method = RequestMethod.POST)
    public ResultVo addCollect(Integer id, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        GetSession g = new GetSession();
        User user = g.userSession(request, response, session);
        Collect c = new Collect();
        System.out.println(user);
        c.setUserId(user.getUserId());
        c.setAutoId(id);
        int i = collectService.addCollect(c);
        if (i>0){
            return new ResultVo(200,"收藏成功");
        }
        return new ResultVo(500,"收藏失败");
    }

    //取消收藏
    @ResponseBody
    @RequestMapping(value = "delCollect", method = RequestMethod.POST)
    public ResultVo delCollect(Integer id, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        GetSession g = new GetSession();
        User user = g.userSession(request, response, session);
        int i = collectService.delCollect(user.getUserId(),id);
        if (i>0){
            return new ResultVo(200,"取消收藏成功");
        }
        return new ResultVo(500,"取消收藏失败");
    }

    //根据用户id查询用户收藏车辆
    @ResponseBody
    @RequestMapping(value = "queryByUserId",method = RequestMethod.GET)
    public List<Name> queryByUserId(Integer userId){
        List<Name> list = collectService.queryByUserId(userId);
        for (Name n : list) {
            n.setPriceMax(autoService.queryMax(n.getCarName()));
            n.setPriceMin(autoService.queryMin(n.getCarName()));
        }
        return list;
    }

    //判断用户是否收藏车型
    @ResponseBody
    @RequestMapping(value = "queryCollect",method = RequestMethod.POST)
    public ResultVo queryCollect(Collect collect,HttpServletRequest request, HttpServletResponse response, HttpSession session){
        GetSession g = new GetSession();
        User user = g.userSession(request, response, session);
        collect.setUserId(user.getUserId());
        System.out.println(collect);
        Collect n = collectService.queryCollect(collect);
        if (n!=null){
            return new ResultVo();
        }
        return new ResultVo(500,"没有收藏");
    }
}
